﻿import urllib,urllib2,cookielib
from bs4 import BeautifulSoup
import settings
import re
def login_website():
    '''csdn'''
    cook_jar=cookielib.CookieJar()
    cookie_support=urllib2.HTTPCookieProcessor(cook_jar)
    opener=urllib2.build_opener(cookie_support,urllib2.HTTPHandler)
    urllib2.install_opener(opener)
    print 'logging'
    
    login_url='http://passport.csdn.net/ajax/accounthandler.ashx?t=log&u=dylinshi&p=123456a&remember=0&f=http%3A%2F%2Fblog.csdn.net%2F&rand=0.363029723724382'
    user_agents = [
            'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6',
            'Opera/9.25 (Windows NT 5.1; U; en)',
            'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
            'Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu)',
            'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12',
            'Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9'
            ]
    headers={
            'User-Agent':user_agents[0],
            'Referer':settings.S_start_urls[0]
            }
    req=urllib2.Request(url=login_url,headers=headers)
    res = urllib2.urlopen(req)
    
    print 'code is :'+str(res.code)
    if res.code<=200:
        print 'login %s success'%settings.S_target_website
    else:
        print 'login %s fail'%settings.S_target_website
        print cook_jar._cookies
    return res

def login_website():                                                                ##
    '''cnblogs'''
    cook_jar=cookielib.CookieJar()
    cookie_support=urllib2.HTTPCookieProcessor(cook_jar)
    opener=urllib2.build_opener(cookie_support,urllib2.HTTPHandler)
    urllib2.install_opener(opener)
    print 'logging'
    
    login_url='http://passport.cnblogs.com/login.aspx?ReturnUrl=http%3a%2f%2fwww.cnblogs.com%2f'
    user_agents = [
            'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6',
            'Opera/9.25 (Windows NT 5.1; U; en)',
            'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
            'Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu)',
            'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12',
            'Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9'
            ]
    post_data=urllib.urlencode({'__EVENTTARGET':'',
                           '__EVENTARGUMENT':'',
                           '__VIEWSTATE':'/wEPDwULLTE1MzYzODg2NzZkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYBBQtjaGtSZW1lbWJlcm1QYDyKKI9af4b67Mzq2xFaL9Bt',
                           '__EVENTVALIDATION':'/wEWBQLWwpqPDQLyj/OQAgK3jsrkBALR55GJDgKC3IeGDE1m7t2mGlasoP1Hd9hLaFoI2G05',
                           'tbUserName':'dylinshi',
                           'tbPassword':'123456a',
                            'btnLogin':'ç»  å½',
                            'txtReturnUrl':'http://www.cnblogs.com/',
                           })
    headers={
            'User-Agent':user_agents[0],
            'Referer':settings.S_start_urls[0]
            }
    req=urllib2.Request(url=login_url,data=post_data,headers=headers)
    res = urllib2.urlopen(req)
    
    print 'code is :'+str(res.code)
    if res.code<=200:
        print 'login %s success'%settings.S_target_website
    else:
        print 'login %s fail'%settings.S_target_website
        print cook_jar._cookies
    return res

def login_website():
    '''51cto'''
    cook_jar=cookielib.CookieJar()
    cookie_support=urllib2.HTTPCookieProcessor(cook_jar)
    opener=urllib2.build_opener(cookie_support,urllib2.HTTPHandler)
    urllib2.install_opener(opener)
    print 'logging'
    login_url='http://home.51cto.com/index.php?s=/Index/doLogin'
    user_agents = ['Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0.1',]
    post_data=urllib.urlencode({'email':'duoylinshi@163.com',
                               'passwd':'123456a',
                               'autologin':'on',
                               'reback':'http%3A%2F%2Fwww.51cto.com%2F',
                               'button.x':36,
                               'button.y':17,
                               })
    headers={
            'User-Agent':user_agents[0],
            'Referer':'http://home.51cto.com/index.php?s=/Index/index/reback/http%253A%252F%252Fwww.51cto.com%252F/'
            }
    req=urllib2.Request(url=login_url,data=post_data,headers=headers)
    res = urllib2.urlopen(req)
    print 'code is :'+str(res.code)
    if res.code<=200:
        print 'login success' 
    else:
        print 'login fail'
    print cook_jar._cookies
    
    login_after_action(res)
    
    return res
def login_after_action(res):
    '''51cto'''
    login_content=res.read()
    patt='src="([\s\S]+?)"'
    src_list=re.findall(patt,login_content)
    for src in src_list:
        url=src
        user_agents = ['Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6',
                ]
        headers={
                'User-Agent':user_agents[0],
                'Referer':'http://www.51cto.com/'
                }
        req=urllib2.Request(url=url,headers=headers)
        res = urllib2.urlopen(req)

def get_login_data():
    '''iteye'''
    url='http://www.iteye.com/login'
    user_agents = ['Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6',]
    headers={'User-Agent':user_agents[0],
            'Referer':'http://www.iteye.com/'}
    req=urllib2.Request(url=url,headers=headers)
    res = urllib2.urlopen(req)
    content=res.read()

    patt='<input [\s\S]+?>'
    input_list=re.findall(patt,content)
    key='authenticity_token'
    return_dic={}
    for minput in input_list:
        if key in minput:
            patt='value="([\s\S]+?)"'
            m=re.search(patt,minput)
            if m:
                value=m.group(1)
                return_dic[key]=value
    return return_dic
def login_website():                                                                ##
    '''iteye'''
    cook_jar=cookielib.CookieJar()
    cookie_support=urllib2.HTTPCookieProcessor(cook_jar)
    opener=urllib2.build_opener(cookie_support,urllib2.HTTPHandler)
    urllib2.install_opener(opener)

    login_extra_dic=get_login_data()
    print login_extra_dic
    print 'logging'
    
    login_url='http://www.iteye.com/login'
    user_agents = [
            'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6',
            'Opera/9.25 (Windows NT 5.1; U; en)',
            'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
            'Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu)',
            'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12',
            'Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9'
            ]
    login_dic={'name':'dylinshi126',
               'password':'123456a',
               'remember_me':'1',
               'button':'ç»ãå½',
               }
    login_dic.update(login_extra_dic)
    post_data=urllib.urlencode(login_dic)
    headers={
            'User-Agent':user_agents[0],
            'Referer':'http://www.iteye.com/login'
            }
    req=urllib2.Request(url=login_url,data=post_data,headers=headers)
    res = urllib2.urlopen(req)
    
    print 'code is :'+str(res.code)
    if res.code<=200:
        print 'login %s success'%settings.S_target_website
    else:
        print 'login %s fail'%settings.S_target_website
    print cook_jar._cookies
    return res

if __name__=='__main__':
    res=login_website()

    url='http://www.iteye.com/topic/29434'
    user_agents = ['Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6',
            ]
    headers={
            'User-Agent':user_agents[0],
            'Referer':'http://www.iteye.com/'
            }
    req=urllib2.Request(url=url,headers=headers)
    res = urllib2.urlopen(req)
    print res.read()







